package com.mycom.controller;

import com.mycom.controller.constant.Code;
import com.mycom.controller.entity.Result;
import com.mycom.exception.BusinessException;
import com.mycom.exception.SystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ProjectExceptionAdvice {
    /**
     * 处理系统异常
     * @param es
     * @return
     */
    @ExceptionHandler(SystemException.class)
    public Result doSystemException(SystemException es){
        //1.记录日志
        //2.发送邮件给开发人员
        //3.发送短信给运维人员
        return new Result(es.getCode(),null,es.getMessage());
    }

    /**
     * 处理业务异常
     * @param be
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public Result doBusinessException(BusinessException be){
        return new Result(be.getCode(),null,be.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result doException(Exception exception){
        //1.记录日志
        //2.发送邮件给开发人员
        //3.发送短信给运维人员
//        System.out.println("11111");
        System.out.println(exception);
//        System.out.println(exception.getMessage());
//        System.out.println(exception.toString());
//        System.out.println(exception.fillInStackTrace());
        return new Result(Code.SYSTEM_UNKNOWN_ERR,null,"系统发生未知异常，请联系管理员!");
    }
}
